package io.renren.modules.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.member.dao.StandardCommDao;
import io.renren.modules.member.entity.ExamEntity;
import io.renren.modules.member.entity.MemberEntity;
import io.renren.modules.member.entity.StandardCommEntity;
import io.renren.modules.member.enums.SportEnums;
import io.renren.modules.member.service.StandardCommService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class StandardCommServiceImpl extends ServiceImpl<StandardCommDao, StandardCommEntity> implements StandardCommService {

    @Autowired
    private StandardRegionAge standardRegionAge;

    private BigDecimal hundred = new BigDecimal("100");

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String sex = (String) params.get("sex");
        String type = (String) params.get("type");

        IPage<StandardCommEntity> page = this.page(
                new Query<StandardCommEntity>().getPage(params, "score", false),
                new QueryWrapper<StandardCommEntity>()
                        .eq(StringUtils.isNotBlank(sex), "sex", sex)
                        .eq(StringUtils.isNotBlank(type), "type", type)
        );
        return new PageUtils(page);
    }

    private StandardCommEntity findByAchvAndSexAndType(String type, String sex, String column, BigDecimal achv) {
        Map<String, Object> params = new HashMap<>();
        params.put("sex", sex);
        params.put("type", type);
        params.put(column, achv);
        if (StringUtils.equals(type, SportEnums.SNAKE_RUN.getCode()) || StringUtils.equals(type, SportEnums.MIDDLE_RUN.getCode())) {
            return baseMapper.findByAchvAndSexAndTypeLess(params);
        }
        return baseMapper.findByAchvAndSexAndTypeMore(params);
    }

    private BigDecimal getStandardAchv(StandardCommEntity entity, String fieldName) {
        try {
            Field field = entity.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            return  (BigDecimal) field.get(entity);
        } catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }

    public BigDecimal getLeadUpScore(ExamEntity examEntity, MemberEntity memberEntity) {
        if (Objects.isNull(examEntity.getLeadUp()) || examEntity.getLeadUp() < 1) {
            return BigDecimal.ZERO;
        }
        String column = standardRegionAge.getGAchv(examEntity.getAge(), SportEnums.LEAD_UP.getCode());
        if (StringUtils.isBlank(column)) {
            return BigDecimal.ZERO;
        }
        StandardCommEntity standardCommEntity = findByAchvAndSexAndType(SportEnums.LEAD_UP.getCode(), memberEntity.getSex(), column, new BigDecimal(examEntity.getLeadUp()));
        if (Objects.isNull(standardCommEntity)) {
            return BigDecimal.ZERO;
        }
        BigDecimal score = standardCommEntity.getScore();
        if (score.compareTo(hundred) == 0) {
            int lastAchv = examEntity.getLeadUp() - getStandardAchv(standardCommEntity, column).intValue();
            if (lastAchv > 0) {
                score = score.add(new BigDecimal(lastAchv));
            }
        }
        return score;
    }

    public BigDecimal getCrankArmScore(ExamEntity examEntity, MemberEntity memberEntity) {
        if (Objects.isNull(examEntity.getCrankArm()) || examEntity.getCrankArm().compareTo(BigDecimal.ZERO) != 1) {
            return BigDecimal.ZERO;
        }
        String column = standardRegionAge.getGAchv(examEntity.getAge(), SportEnums.CRANK_ARM.getCode());
        if (StringUtils.isBlank(column)) {
            return BigDecimal.ZERO;
        }
        StandardCommEntity standardCommEntity = findByAchvAndSexAndType(SportEnums.CRANK_ARM.getCode(), memberEntity.getSex(), column, examEntity.getCrankArm());
        if (Objects.isNull(standardCommEntity)) {
            return BigDecimal.ZERO;
        }
        BigDecimal score = standardCommEntity.getScore();
        if (score.compareTo(hundred) == 0) {
            int lastAchv = examEntity.getCrankArm().subtract(getStandardAchv(standardCommEntity, column)).divide(new BigDecimal("5")).intValue();
            if (lastAchv > 0) {
                score = score.add(new BigDecimal(lastAchv));
            }
        }
        return score;
    }

    public BigDecimal getPushUpScore(ExamEntity examEntity, MemberEntity memberEntity) {
        if (Objects.isNull(examEntity.getPushUp()) || examEntity.getPushUp() < 1) {
            return BigDecimal.ZERO;
        }
        String column = standardRegionAge.getGAchv(examEntity.getAge(), SportEnums.PUSH_UP.getCode());
        if (StringUtils.isBlank(column)) {
            return BigDecimal.ZERO;
        }
        StandardCommEntity standardCommEntity = findByAchvAndSexAndType(SportEnums.PUSH_UP.getCode(), memberEntity.getSex(), column, new BigDecimal(examEntity.getPushUp()));
        if (Objects.isNull(standardCommEntity)) {
            return BigDecimal.ZERO;
        }
        BigDecimal score = standardCommEntity.getScore();
        if (score.compareTo(hundred) == 0) {
            int lastAchv = (examEntity.getPushUp() - getStandardAchv(standardCommEntity, column).intValue()) / 2;
            if (lastAchv > 0) {
                score = score.add(new BigDecimal(lastAchv));
            }
        }
        return score;
    }

    public BigDecimal getSitUpScore(ExamEntity examEntity, MemberEntity memberEntity) {
        if (Objects.isNull(examEntity.getSitUp()) || examEntity.getSitUp() < 1) {
            return BigDecimal.ZERO;
        }
        String column = standardRegionAge.getGAchv(examEntity.getAge(), SportEnums.SIT_UP.getCode());
        if (StringUtils.isBlank(column)) {
            return BigDecimal.ZERO;
        }
        StandardCommEntity standardCommEntity = findByAchvAndSexAndType(SportEnums.SIT_UP.getCode(), memberEntity.getSex(), column, new BigDecimal(examEntity.getSitUp()));
        if (Objects.isNull(standardCommEntity)) {
            return BigDecimal.ZERO;
        }
        BigDecimal score = standardCommEntity.getScore();
        if (score.compareTo(hundred) == 0) {
            int lastAchv = (examEntity.getSitUp() - getStandardAchv(standardCommEntity, column).intValue()) / 2;
            if (lastAchv > 0) {
                score = score.add(new BigDecimal(lastAchv));
            }
        }
        return score;
    }

    public BigDecimal getSnakeScore(ExamEntity examEntity, MemberEntity memberEntity) {
        if (Objects.isNull(examEntity.getSnakeRun()) || examEntity.getSnakeRun().compareTo(BigDecimal.ZERO) != 1) {
            return BigDecimal.ZERO;
        }
        String column = standardRegionAge.getGAchv(examEntity.getAge(), SportEnums.SNAKE_RUN.getCode());
        if (StringUtils.isBlank(column)) {
            return BigDecimal.ZERO;
        }
        StandardCommEntity standardCommEntity = findByAchvAndSexAndType(SportEnums.SNAKE_RUN.getCode(), memberEntity.getSex(), column, examEntity.getSnakeRun());
        if (Objects.isNull(standardCommEntity)) {
            return BigDecimal.ZERO;
        }
        BigDecimal score = standardCommEntity.getScore();
        if (score.compareTo(hundred) == 0) {
            int lastAchv = getStandardAchv(standardCommEntity, column).subtract(examEntity.getSnakeRun()).multiply(new BigDecimal("10")).intValue() ;
            if (lastAchv > 0) {
                score = score.add(new BigDecimal(lastAchv));
            }
        }
        return score;
    }

    public BigDecimal getMiddleRunScore(ExamEntity examEntity, MemberEntity memberEntity) {
        if (Objects.isNull(examEntity.getMiddleRun()) || examEntity.getMiddleRun().compareTo(BigDecimal.ZERO) != 1) {
            return BigDecimal.ZERO;
        }
        String column = standardRegionAge.getGAchv(examEntity.getAge(), SportEnums.MIDDLE_RUN.getCode());
        if (StringUtils.isBlank(column)) {
            return BigDecimal.ZERO;
        }
        StandardCommEntity standardCommEntity = findByAchvAndSexAndType(SportEnums.MIDDLE_RUN.getCode(), memberEntity.getSex(), column, examEntity.getMiddleRun());
        if (Objects.isNull(standardCommEntity)) {
            return BigDecimal.ZERO;
        }
        BigDecimal score = standardCommEntity.getScore();
        if (score.compareTo(hundred) == 0) {
            BigDecimal last = getStandardAchv(standardCommEntity, column).subtract(examEntity.getMiddleRun()); // 少出时间
            int lastAchv = (last.intValue() * 60 + last.subtract(new BigDecimal(last.intValue() + "")).multiply(new BigDecimal("100")).intValue() ) / 5;
            if (lastAchv > 0) {
                score = score.add(new BigDecimal(lastAchv));
            }
        }
        return score;
    }

//    public static void main(String args[]) {
//        BigDecimal last = new BigDecimal("1.40");
//        int lastAchv = (last.intValue() * 60 + last.subtract(new BigDecimal(last.intValue() + "")).multiply(new BigDecimal("100")).intValue() ) / 5;
//        System.out.println("----------------" + lastAchv);
//    }
}
